<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>图表生成工具</title>
    <script src="../js/echarts.min.js"></script>
    <link href="../css/all.min.css" rel="stylesheet">
    <style>
        :root {
            --primary: #6366f1;
            --primary-light: #818cf8;
            --primary-dark: #4f46e5;
            --text: #374151;
            --text-light: #6b7280;
            --bg: #f9fafb;
            --card-bg: #ffffff;
            --border: #e5e7eb;
            --shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
        }

        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background-color: var(--bg);
            color: var(--text);
            line-height: 1.5;
            padding: 0;
            height: 100vh;
        }

        .container {
            margin: 0 auto;
            padding: 2rem;
        }

        .form-container {
            background-color: var(--card-bg);
            border-radius: 0.75rem;
            box-shadow: var(--shadow);
            padding: 2rem;
            margin-bottom: 2rem;
        }

        .form-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
            gap: 1.5rem;
            margin-bottom: 1.5rem;
        }

        .form-group {
            margin-bottom: 1rem;
        }

        label {
            display: block;
            margin-bottom: 0.5rem;
            font-size: 0.875rem;
            font-weight: 500;
            color: var(--text);
        }

        input[type="text"],
        select {
            width: 100%;
            padding: 0.75rem 1rem;
            border: 1px solid var(--border);
            border-radius: 0.5rem;
            font-size: 0.875rem;
            transition: all 0.2s ease;
            background-color: var(--card-bg);
            color: var(--text);
        }

        input[type="text"]:focus,
        select:focus {
            outline: none;
            border-color: var(--primary-light);
            box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.1);
        }

        .form-footer {
            display: flex;
            justify-content: flex-end;
            align-items: center;
            gap: 1rem;
            margin-top: 1rem;
        }

        select {
            appearance: none;
            padding-right: 2.5rem;
        }

        button {
            display: inline-flex;
            align-items: center;
            justify-content: center;
            padding: 0.75rem 1.5rem;
            border-radius: 0.5rem;
            font-size: 0.875rem;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.2s ease;
            border: none;
            background-color: var(--primary);
            color: white;
            box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
            width: 200px;
        }

        button:hover {
            background-color: var(--primary-dark);
            transform: translateY(-1px);
        }

        button:active {
            transform: translateY(0);
        }

        button i {
            margin-right: 0.5rem;
            font-size: 0.875rem;
        }

        #chart-container {
            background-color: var(--card-bg);
            border-radius: 0.75rem;
            box-shadow: var(--shadow);
            width: 100%;
            height: 400px;
            padding: 1rem;
            transition: all 0.3s ease;
        }

        .tooltip {
            position: relative;
            display: inline-block;
            margin-left: 0.5rem;
            color: var(--primary-light);
            cursor: help;
        }

        .tooltip .tooltip-text {
            visibility: hidden;
            width: 200px;
            background-color: #333;
            color: #fff;
            text-align: center;
            border-radius: 6px;
            padding: 5px;
            position: absolute;
            z-index: 1;
            bottom: 125%;
            left: 50%;
            transform: translateX(-50%);
            opacity: 0;
            transition: opacity 0.3s;
            font-size: 0.75rem;
            font-weight: normal;
        }

        .tooltip:hover .tooltip-text {
            visibility: visible;
            opacity: 1;
        }

        @media (max-width: 768px) {
            .container {
                padding: 1rem;
            }
            
            .form-grid {
                grid-template-columns: 1fr;
            }
            
            .form-footer {
                flex-direction: column;
                align-items: stretch;
            }
            
            button {
                width: 200px;
            }
        }
    </style>
</head>

<body>
    <div class="container">
        <div class="form-container">
            <div class="form-grid">
                <div class="form-group">
                    <label for="chart-title">
                        图表标题
                        <span class="tooltip">
                            <i class="fas fa-info-circle"></i>
                            <span class="tooltip-text">输入您想显示的图表标题</span>
                        </span>
                    </label>
                    <input type="text" id="chart-title" value="水果销售统计" placeholder="例如: 月度销售数据">
                </div>
                <div class="form-group">
                    <label for="data-unit">
                        数据单位
                        <span class="tooltip">
                            <i class="fas fa-info-circle"></i>
                            <span class="tooltip-text">输入数据的单位(如: 个, 元, 公斤等)</span>
                        </span>
                    </label>
                    <input type="text" id="data-unit" value="个" placeholder="例如: 个, 元, 公斤">
                </div>
            </div>

            <div class="form-grid">
                <div class="form-group">
                    <label for="x-axis">
                        X 轴数据
                        <span class="tooltip">
                            <i class="fas fa-info-circle"></i>
                            <span class="tooltip-text">输入X轴数据，用中文顿号分隔(如: 苹果、香蕉、橙子)</span>
                        </span>
                    </label>
                    <input type="text" id="x-axis" value="苹果、香蕉、橙子" placeholder="例如: 一月、二月、三月">
                </div>
                <div class="form-group">
                    <label for="y-axis">
                        Y 轴数据
                        <span class="tooltip">
                            <i class="fas fa-info-circle"></i>
                            <span class="tooltip-text">输入Y轴数据，用中文顿号分隔(如: 10、20、30)</span>
                        </span>
                    </label>
                    <input type="text" id="y-axis" value="10、20、30" placeholder="例如: 100、200、300">
                </div>
            </div>

            <div class="form-footer">
                <select id="chart-type">
                    <option value="bar">柱状图</option>
                    <option value="line">折线图</option>
                    <option value="pie">饼状图</option>
                </select>
                <button id="generate-chart">
                    <i class="fas fa-magic"></i> 生成图表
                </button>
            </div>
        </div>

        <div id="chart-container"></div>
    </div>

    <script>
        const chartTypeSelect = document.getElementById('chart-type');
        const generateChartButton = document.getElementById('generate-chart');
        const xAxisInput = document.getElementById('x-axis');
        const yAxisInput = document.getElementById('y-axis');
        const chartTitleInput = document.getElementById('chart-title');
        const dataUnitInput = document.getElementById('data-unit');
        const chartContainer = document.getElementById('chart-container');

        // 初始化图表
        let myChart = echarts.init(chartContainer);
        
        // 设置响应式
        window.addEventListener('resize', function() {
            myChart.resize();
        });

        // 生成图表函数
        function generateChart() {
            const chartType = chartTypeSelect.value;
            const xAxisData = xAxisInput.value.split('、').filter(item => item.trim() !== '');
            const yAxisData = yAxisInput.value.split('、').map(Number).filter(item => !isNaN(item));
            const chartTitle = chartTitleInput.value || '未命名图表';
            const dataUnit = dataUnitInput.value || '';
            
            // 验证数据
            if (xAxisData.length === 0 || yAxisData.length === 0 || xAxisData.length !== yAxisData.length) {
                alert('X轴和Y轴数据数量不匹配或为空，请检查输入！');
                return;
            }

            let option;

            const colors = ['#6366f1', '#8b5cf6', '#a855f7', '#d946ef', '#ec4899'];
            const lightColors = ['#c7d2fe', '#ddd6fe', '#e9d5ff', '#f5d0fe', '#fbcfe8'];

            if (chartType === 'bar') {
                option = {
                    title: {
                        text: chartTitle,
                        left: 'center',
                        textStyle: {
                            fontSize: 18,
                            fontWeight: 'bold',
                            color: '#374151'
                        },
                        subtext: '数据可视化展示',
                        subtextStyle: {
                            fontSize: 12,
                            color: '#6b7280'
                        }
                    },
                    color: colors,
                    tooltip: {
                        trigger: 'axis',
                        axisPointer: {
                            type: 'shadow'
                        },
                        formatter: '{b}<br/>{a}: {c}' + dataUnit
                    },
                    grid: {
                        left: '3%',
                        right: '4%',
                        bottom: '3%',
                        containLabel: true
                    },
                    xAxis: {
                        type: 'category',
                        data: xAxisData,
                        axisLine: {
                            lineStyle: {
                                color: '#e5e7eb'
                            }
                        },
                        axisLabel: {
                            color: '#6b7280',
                            fontSize: 12
                        },
                        axisTick: {
                            alignWithLabel: true
                        }
                    },
                    yAxis: {
                        type: 'value',
                        axisLine: {
                            show: true,
                            lineStyle: {
                                color: '#e5e7eb'
                            }
                        },
                        axisLabel: {
                            color: '#6b7280',
                            fontSize: 12,
                            formatter: '{value}' + dataUnit
                        },
                        splitLine: {
                            lineStyle: {
                                color: '#f3f4f6',
                                type: 'dashed'
                            }
                        }
                    },
                    series: [{
                        name: '数量',
                        data: yAxisData,
                        type: 'bar',
                        barWidth: '60%',
                        itemStyle: {
                            borderRadius: [4, 4, 0, 0],
                            color: function(params) {
                                return new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                                    { offset: 0, color: colors[params.dataIndex % colors.length] },
                                    { offset: 1, color: lightColors[params.dataIndex % lightColors.length] }
                                ]);
                            }
                        },
                        label: {
                            show: true,
                            position: 'top',
                            color: '#374151',
                            formatter: function(params) {
                                return params.value + dataUnit;
                            }
                        },
                        emphasis: {
                            itemStyle: {
                                shadowBlur: 10,
                                shadowOffsetX: 0,
                                shadowColor: 'rgba(0, 0, 0, 0.3)'
                            }
                        }
                    }]
                };
            } else if (chartType === 'line') {
                option = {
                    title: {
                        text: chartTitle,
                        left: 'center',
                        textStyle: {
                            fontSize: 18,
                            fontWeight: 'bold',
                            color: '#374151'
                        },
                        subtext: '数据趋势分析',
                        subtextStyle: {
                            fontSize: 12,
                            color: '#6b7280'
                        }
                    },
                    color: colors,
                    tooltip: {
                        trigger: 'axis',
                        formatter: '{b}<br/>{a}: {c}' + dataUnit
                    },
                    grid: {
                        left: '3%',
                        right: '4%',
                        bottom: '3%',
                        containLabel: true
                    },
                    xAxis: {
                        type: 'category',
                        boundaryGap: false,
                        data: xAxisData,
                        axisLine: {
                            lineStyle: {
                                color: '#e5e7eb'
                            }
                        },
                        axisLabel: {
                            color: '#6b7280',
                            fontSize: 12
                        },
                        axisTick: {
                            alignWithLabel: true
                        }
                    },
                    yAxis: {
                        type: 'value',
                        axisLine: {
                            show: true,
                            lineStyle: {
                                color: '#e5e7eb'
                            }
                        },
                        axisLabel: {
                            color: '#6b7280',
                            fontSize: 12,
                            formatter: '{value}' + dataUnit
                        },
                        splitLine: {
                            lineStyle: {
                                color: '#f3f4f6',
                                type: 'dashed'
                            }
                        }
                    },
                    series: [{
                        name: '数量',
                        data: yAxisData,
                        type: 'line',
                        smooth: true,
                        symbol: 'circle',
                        symbolSize: 8,
                        lineStyle: {
                            width: 3
                        },
                        label: {
                            show: true,
                            position: 'top',
                            color: '#374151',
                            formatter: function(params) {
                                return params.value + dataUnit;
                            }
                        },
                        areaStyle: {
                            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                                { offset: 0, color: 'rgba(99, 102, 241, 0.3)' },
                                { offset: 1, color: 'rgba(99, 102, 241, 0.1)' }
                            ])
                        },
                        emphasis: {
                            focus: 'series',
                            itemStyle: {
                                borderWidth: 2,
                                borderColor: '#fff',
                                shadowBlur: 10,
                                shadowColor: 'rgba(0, 0, 0, 0.3)'
                            }
                        }
                    }]
                };
            } else if (chartType === 'pie') {
                const pieData = xAxisData.map((item, index) => ({
                    value: yAxisData[index],
                    name: item
                }));
                
                option = {
                    title: {
                        text: chartTitle,
                        left: 'center',
                        textStyle: {
                            fontSize: 18,
                            fontWeight: 'bold',
                            color: '#374151'
                        },
                        subtext: '数据占比分布',
                        subtextStyle: {
                            fontSize: 12,
                            color: '#6b7280'
                        }
                    },
                    color: colors,
                    tooltip: {
                        trigger: 'item',
                        formatter: '{a} <br/>{b}: {c}' + dataUnit + ' ({d}%)'
                    },
                    legend: {
                        orient: 'horizontal',
                        bottom: 0,
                        data: xAxisData,
                        textStyle: {
                            color: '#6b7280'
                        }
                    },
                    series: [{
                        name: '占比',
                        type: 'pie',
                        radius: ['40%', '70%'],
                        avoidLabelOverlap: false,
                        itemStyle: {
                            borderRadius: 6,
                            borderColor: '#fff',
                            borderWidth: 2
                        },
                        label: {
                            show: true,
                            formatter: function(params) {
                                return params.name + ': ' + params.value + dataUnit;
                            },
                            fontSize: 12,
                            color: '#374151'
                        },
                        labelLine: {
                            show: true,
                            smooth: 0.2,
                            length: 10,
                            length2: 20
                        },
                        emphasis: {
                            label: {
                                show: true,
                                fontSize: 14,
                                fontWeight: 'bold'
                            },
                            itemStyle: {
                                shadowBlur: 10,
                                shadowOffsetX: 0,
                                shadowColor: 'rgba(0, 0, 0, 0.3)'
                            }
                        },
                        data: pieData
                    }]
                };
            }

            myChart.setOption(option, true);
        }

        // 添加事件监听
        generateChartButton.addEventListener('click', generateChart);
        
        // 初始生成图表
        generateChart();

        // 重写事件监听器，将其标记为 passive
        const originalAddEventListener = EventTarget.prototype.addEventListener;
        EventTarget.prototype.addEventListener = function (type, listener, options) {
            if (type === 'mousewheel' && typeof options === 'object' && options.passive === undefined) {
                options = { ...options, passive: true };
            }
            return originalAddEventListener.call(this, type, listener, options);
        };
    </script>
</body>

</html>